package com.yunnuo.system.dept.service.impl;

import com.yunnuo.system.comm.util.StringUtils;
import com.yunnuo.system.dept.dao.IDeptDao;
import com.yunnuo.system.dept.entity.Dept;
import com.yunnuo.system.dept.service.IDeptService;
import com.yunnuo.system.users.dao.IUserDao;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by w_weihf on 2018/8/3.
 */
@Service
public class DeptServiceImpl implements IDeptService {

    @Autowired
    private IDeptDao deptDao;
    @Autowired
    private IUserDao userDao;

    @Override
    public void addVO(Dept vo) throws ServiceException {
        deptDao.saveAndFlush(vo);
    }

    @Override
    public void updateVO(Dept vo) throws ServiceException {
        deptDao.saveAndFlush(vo);
    }

    @Override
    public void delList(String[] ids) throws ServiceException {

    }

    @Override
    public void deleteVo(String id) throws ServiceException {
        deptDao.delete(id);
    }

    @Override
    public List<Dept> findList() throws ServiceException {
        return deptDao.findAll();
    }

    @Override
    public List<?> findList(Dept vo, String other) throws ServiceException {
        return deptDao.findAll();
    }

    @Override
    public Dept findVO(String id) throws ServiceException {
        return deptDao.findOne(id);
    }

    @Override
    public int selectDeptCount(String id) {

        return deptDao.countDeptByParentId(id);
    }

    @Override
    public int checkDeptExistUser(String id) {

        return userDao.countUsersByDeptId(id);
    }

    @Override
    public List<Map<String, Object>> selectDeptTree() {
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        List<Dept> deptList =deptDao.findAll();

        for (Dept dept : deptList)
        {
            if (dept.getDeptStatus()==0)
            {
                Map<String, Object> deptMap = new HashMap<>();
                deptMap.put("id", dept.getId());
                deptMap.put("pId", dept.getParentId());
                deptMap.put("name", dept.getDeptName());
                trees.add(deptMap);
            }
        }
        return trees;
    }

    @Override
    public int checkDeptNameUnique(Dept dept) {
        int count1=0 ;
        if(null!=dept.getId()&&!"".equals(dept.getId())){
            count1= deptDao.countAllByIdAndAndDeptName(dept.getId(),dept.getDeptName());
            if (count1==1){
                return 0;
            }
        }
        return deptDao.countDeptByDeptName(dept.getDeptName());
    }
    /**
     * 保存部门信息
     * @param dept
     */
    @Override
    public void save(Dept dept) {
        try {
            /*
             * 批量上传部门时表格中填写的父级部门是部门名称，在这里处理
             */
            if((null==dept.getParentId() || "".equals(dept.getParentId())) &&
                    (null!=dept.getParentName() && !"".equals(dept.getParentName()))){
                dept.setParentId(deptDao.findAllByDeptName(dept.getParentName()).getId());
            }
            deptDao.save(dept);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<String> selectSubordinateDept(String deptId) {
        //查询当前dept
        Dept dept = deptDao.findOne(deptId);
        //查询下属部门
        List<Dept> list = deptDao.findAllByDeptCodeLike(dept.getDeptCode()+"%");
        List<String > list2 =new ArrayList<>();
        for(int i=0;i<list.size();i++){
            list2.add(list.get(i).getId());
        }
        return list2;
    }
}
